<?php
/**
 * 用户商店
 * @author zengnz
 *
 */
class ShopAction extends CommonAction
{
	public function index() {
		$uId = $this -> getUid();
		
		$ShopLogic = D('Shop', 'Logic');
		$shops = $ShopLogic -> getUserShops($uId);
		$shopTypes = $ShopLogic -> getShopTypes();
		
		$shopList = $temp = array();
		$shopCount = 0;
		if (is_array($shops) && !empty($shops)) {
			$shopCount = count($shops);
			
			foreach ($shops as $val) {
				$temp[$val['type']][] = $val;
			}
				
			foreach ($temp as $key => $val) {
				$shopList[$key]['name'] = $shopTypes[$key];
				$shopList[$key]['num'] = count($val);
				$shopList[$key]['shops'] = $val;
			}
		}
		
		$this -> assign('shops', $shopList);
		$this -> assign('shopTypes', $shopTypes);
		$this -> assign('shopCount', count($shops));
		$this -> assign('action', 'shop');
		$this -> display();
	}
	
	/**
	 * 开店
	 */
	public function openShop() {
		$shopList = D('Shop', 'Logic') -> getShopTypes();
		
		$brandList = D('CarCategory', 'Logic') -> getSubBrands();
		
		$provinces = getProvinces();
		
		$this -> assign('province', $provinces);
		$this -> assign('shopList', $shopList);
		$this -> assign('brandList', $brandList);
		$this -> display();
	}
	
	public function doOpenShop() {
		$params = $_POST;
		if (is_array($params) && !empty($params)) {
			$Model = new Model();
			
			$ShopLogic = D('Shop', 'Logic');
			$uId = $this -> getUid();
			$customerName = $this -> getCustomer();
			
			$cost = $ShopLogic -> getOpenShopCost($params['type']);
			if ($cost < 0)
				$this -> error('获取消费积分失败！');
			
			$IntegrationLogic = D('Integration', 'Logic');
			$checkEnough = $IntegrationLogic -> checkInteEnough($uId, $cost);
			if ($checkEnough != 1)
				$this -> error('积分不足！');
			
			// 事务开启
			$Model -> startTrans();
			
			$ret = $ShopLogic -> addShop();
			if (!$ret)
				$this -> error($ShopLogic -> getError());
			
			// 扣积分
			$inteRet = $IntegrationLogic -> updateIntegration($uId, $cost, 2);
			if (!$inteRet) {
				$Model -> rollback();
				$this -> error('扣除积分失败！');
			}
				
			// 添加积分明细
			$inteDetail = $IntegrationLogic -> addIntegrationDetail($uId, $cost, 2, '开店消费，店铺标识：'.$params['type']);
			if (!$inteDetail) {
				$Model -> rollback();
				$this -> error('添加积分消费明细失败！');
			}
			
			// 添加日志
			$ret = D('Log', 'Logic') -> addActionLog($uId, $customerName, 1, '用户开设店铺，店铺名称：'. $params['name']);
			if (!$ret) {
				$Model -> rollback();
				$this -> error('添加日志失败！');
			}
				
			$Model -> commit();
			
			$this -> redirect('index');			
		} else {
			$this -> error('操作有误！');
		} 
	}
	
	/**
	 * ajax获取开设店铺价格
	 */
	public function ajaxGetShopPrice() {
		$ShopLogic = D('Shop', 'Logic');
		$type = $this -> getParam('type');
		$cost = $ShopLogic -> getOpenShopCost($type);
		
		$IntegrationLogic = D('Integration', 'Logic');
		$uId = $this -> getUid();
		$enough = $IntegrationLogic -> checkInteEnough($uId, $cost);
		
		echo json_encode(array('cost'=>$cost, 'enough'=>$enough));
	}
	
	/**
	 * 获取省份下的城市
	 */
	public function getCitys() {
		$pCode = $_GET['p_code'];
		$cityList = getProvinceAreas();
		
		$options = array();
		if (isset($cityList[$pCode])) {
			$citys = $cityList[$pCode];
			if (is_array($cityList) && !empty($cityList)) {
				foreach ($citys as $key=>$val){
					$options[] = '<option value='.$key.'>'.$val.'</option>';
				}
			}
		}
		
		$options = implode('',$options);
		echo $options;
	}
	
	/**
	 * 我的店铺
	 */
	public function myShop() {
		$uId = $this -> getUid();
		$ShopLogic = D('Shop', 'Logic');
		$shops = $ShopLogic -> getUserShops($uId);
		$shopTypes = $ShopLogic -> getShopTypes();
		
		$shopList = $temp = array();
		if (is_array($shops) && !empty($shops)) {
			foreach ($shops as $val) {
				$temp[$val['type']][] = $val;
			}
			
			foreach ($temp as $key => $val) {
				$shopList[$key]['name'] = $shopTypes[$key];
				$shopList[$key]['num'] = count($val);
				$shopList[$key]['shops'] = $val;
			}
		}
		
		$this -> assign('shops', $shopList);
		$this -> assign('shopTypes', $shopTypes);
		$this -> display();
	}
	
	/**
	 * 商铺信息
	 */
	public function shopInfo() {
		$shopId = $this -> getParam('shopId');
		$uId = $this -> getUid();
		
		//判断商店是否存在，防止构造URL
		
		$ShopLogic = D('Shop', 'Logic');
		$shopInfo = $ShopLogic -> getShopInfo($shopId, $uId);
		
		$shopTypes = $ShopLogic -> getShopTypes();
		$typeName = $shopInfo['type'] ? $shopTypes[$shopInfo['type']] : '店铺信息不存在！';

		$this -> assign('typeName', $typeName);
		$this -> assign('shopInfo', $shopInfo);
		$this -> assign('action', 'shop');
		$this -> assign('shopId', $shopId);
		$this -> display();
	}
	
	/**
	 * 商铺产品类别管理
	 */
	public function productCategory() {
		
		
	}
	
	/**
	 * 添加产品类型
	 */
	public function addProductCategory() {
		$param = $_POST;
		
		$ProductCategoryLogic = D('productCategory', 'Logic');
		//判断商店是否存在，防止构造URL
		
		//业务规则
		
		$ret = $ProductCategoryLogic -> addProductCategory($param['shopId'],$param['pid'],$param['name']);
		$backArr = array();
		if (!$ret)
			$backArr = array('ret'=>0,'msg'=>'对不起，添加产品类别失败，请重试！');
		
		$backArr = array('ret'=>1,'msg'=>'恭喜您，添加产品类别成功！');
		echo json_encode($backArr);
	}
	
	/**
	 * 产品管理
	 */
	public function product() {
		$shopId = $_GET['shop_id'];
		if (!$shopId)
			$this -> error('参数错误！');
		
		$uid = $this -> getUid();
		// 判断商店是否存在，防止构造URL
		$shopInfo = D('shop', 'Logic') -> getShopInfo($shopId, $uid);
		if (!$shopInfo)
			$this -> error('商铺信息不存在！');
		
		$categorys = D('productCategory', 'Logic') -> getProductCategory($uid, $shopId);
		
		$plist = '<div style="margin:7px 10px;">父级栏目:<select id="p_id" name="pid"><option value="">--请选择--</option><option value="0">一级栏目</option>';
		
		$parentCategory = $subCategory = array(); 
		if (is_array($categorys) && !empty($categorys)) {
			foreach ($categorys as $key => $val) {
				if ($val['pid'] == 0) {
					$plist .= '<option value="'.$val['id'].'">'.$val['name'].'</option>';
					$parentCategory[$val['id']]['par'] = $val;
				} else{
					$parentCategory[$val['pid']]['sub'][$val['id']] = $val;
				}
			}
		}
		
		$plist .= '</select></div>';
		
		$this -> assign('parentCategory', $parentCategory);
		$this -> assign('subCategory', $subCategory);
		
		$this -> assign('plist', $plist);
		$this -> assign('shopId', $shopId);
		$this -> assign('err', 0);
		$this -> display();
	}
	
	/**
	 * 添加产品
	 */
	public function addProduct() {
		if ($this -> isPost()) {
			$param = $_POST;
			import("@.ORG.UploadFile");
			$File = new UploadFile();
			
			$ret = $File -> uploadOne($param['file'], 'Public/upload/file');
		}
		
		$uid = $this -> getUid();
		$shopId = $_POST['shopId'];
		
		$categorys = D('productCategory', 'Logic') -> getProductCategory($uid, $shopId);
		
		$this -> assign('categorys', $categorys);
		$this -> display();
	}
	
}